package com.vegetable.modules.controller.baseData;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.baseData.ProductPackMaterial;
import com.vegetable.modules.entity.baseData.ProductSpec;
import com.vegetable.modules.entity.baseData.Products;
import com.vegetable.modules.service.baseData.IProductPackMaterialService;
import com.vegetable.modules.service.baseData.IProductSpecService;
import com.vegetable.modules.service.baseData.IProductsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 自营包装耗材标准 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-05
 */
@Slf4j
@RestController
@RequestMapping("/productPackMaterial")
@AllArgsConstructor
@Api(value = "自营包装耗材标准", tags = "自营包装耗材标准")
public class ProductPackMaterialController {

    private IProductsService productsService;

    private IProductSpecService productSpecService;

    private IProductPackMaterialService productPackMaterialService;
    /**
     * 所有自营包装耗材标准列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据产地商品id查询自营包装耗材标准列表", notes = "根据产地商品id查询自营包装耗材标准列表")
    public R list(@RequestParam Map<String, Object> params){
        String producerId = (String)params.get("producerId");
        if (StringUtils.isBlank(producerId)){
            return R.fail("商品id不能为空");
        }
        log.info("根据产地商品id查询自营包装耗材标准列表{}", JSON.toJSONString(params));
        PageUtils page = productPackMaterialService.queryPage(params);

        return R.data( page);
    }

    /**
     * 新增自营包装耗材标准
     */
    @SysLog(value = "新增自营包装耗材标准")
    @PostMapping("/save")
    @ApiOperation(value = "新增产地商品的自营包装耗材标准", notes = "新增产地商品的自营包装耗材标准")
    public R save(@RequestBody ProductPackMaterial packMaterial){
        log.info("新增自营包装耗材标准{}", JSON.toJSONString(packMaterial));
        if (packMaterial.getProducerId() == null){
            return R.fail("商品id不能为空");
        }
        if(1 == packMaterial.getSign()){
            packMaterial.setCostClauseName("加工单价");
        }else{
            Products products = productsService.getById(packMaterial.getCostClauseId());
            if (products == null){
                return R.fail("该包装类产地商品不存在");
            }
            packMaterial.setCostClauseName(products.getCommonName());
        }
        List<ProductSpec> specList = productSpecService.queryByProductId(packMaterial.getCostClauseId());
        if(CollectionUtil.isNotEmpty(specList) && Objects.isNull(packMaterial.getSpecsId())){
            return R.fail(packMaterial.getCostClauseName() + "有" + specList.size() + "种包装, 不可以不选");
        }
        if(Objects.nonNull(packMaterial.getSpecsId())){
            ProductSpec productSpec = productSpecService.getById(packMaterial.getSpecsId());
            if (productSpec == null){
                return R.fail("包装规格不存在");
            }
            packMaterial.setSpecsName(productSpec.getName());
        }
        productPackMaterialService.save(packMaterial);
        return R.status(true);
    }

    /**
     * 修改自营包装耗材标准
     */
    @SysLog(value = "修改自营包装耗材标准")
    @PostMapping("/update")
    @ApiOperation(value = "修改产地商品的自营包装耗材标准", notes = "修改产地商品的自营包装耗材标准")
    public R update(@RequestBody ProductPackMaterial packMaterial){
        log.info("修改产地商品的自营包装耗材标准{}", JSON.toJSONString(packMaterial));
        if (packMaterial.getProducerId() == null || packMaterial.getId() == null){
            return R.fail("商品id和自营包装耗材标准id都不能为空");
        }
        if(1 == packMaterial.getSign()){
            packMaterial.setCostClauseName("加工单价");
        }else{
            Products products = productsService.getById(packMaterial.getCostClauseId());
            if (products == null){
                return R.fail("该包装类产地商品不存在");
            }
            packMaterial.setCostClauseName(products.getCommonName());
        }
        List<ProductSpec> specList = productSpecService.queryByProductId(packMaterial.getProducerId());
        if(CollectionUtil.isNotEmpty(specList) && Objects.isNull(packMaterial.getSpecsId())){
            return R.fail(packMaterial.getCostClauseName() + "有" + specList.size() + "种包装, 不可以不选");
        }
        if(Objects.nonNull(packMaterial.getSpecsId())){
            ProductSpec productSpec = productSpecService.getById(packMaterial.getSpecsId());
            if (productSpec == null){
                return R.fail("包装规格不存在");
            }
            packMaterial.setSpecsName(productSpec.getName());
        }
        productPackMaterialService.updateById(packMaterial);
        return R.status(true);
    }

    /**
     * 删除自营包装耗材标准
     */
    @SysLog(value = "删除自营包装耗材标准")
    @GetMapping("/delete")
    @ApiOperation(value = "根据自营包装耗材标准id删除自营包装耗材标准", notes = "根据自营包装耗材标准id删除自营包装耗材标准")
    public R delete(Long id){
        productPackMaterialService.removeById(id);
        return R.status(true);
    }
}
